package com.example.watermark;

import com.example.model.WaterSensor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WaterMarkDemo
 * Package: com.example.watermark
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-23
 * Time: 21:10
 */

//水位线生成策略
public class WaterMarkDemo {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> map = env.socketTextStream("hadoop103", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] s = value.split(" ");

                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));
                    }
                });

        //水位线分配策略
        WatermarkStrategy<WaterSensor> watermark = WatermarkStrategy
                //有序内置水位线
                .<WaterSensor>forMonotonousTimestamps()
                //参数2 时间提取器
                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                    //参数1 传来的数据   参数2 当前的时间戳 已经记录的时间戳
                    @Override
                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                        //将Ts字段 作为数据的时间戳 作为事件时间
                        System.out.println("element=" + element + ", record=" + recordTimestamp);
                        return element.getTs() * 1000L;
                    }
                });
        //接收到数据  DataStream 就要开始设置水位线
        //调用assignTimestampsAndWatermarks 分配 时间策略 和 设置水位线 参数是WatermarkStrategy
        //WatermarkStrategy 设置水位线策略 （顺序 还是 乱序）, 时间提取器 从数据中获取时间戳 作为事件时间
        //返回数据流 SingleOutputStreamOperator 往下调用

        //设置水位线之后 KeyBy操作 才能开窗口 设置窗口运行策略 是 一定是事件时间驱动 然后调用的窗口函数
        //时间范围是前闭后开的 而且是整数形式
        //测试的时候 并行度要设置为1 ：：但是经过KeyBy会根据Id分区才对 为什么没有输出结果

        SingleOutputStreamOperator<WaterSensor> waterSensorSingleOutputStreamOperator =
                map.assignTimestampsAndWatermarks(watermark);

        KeyedStream<WaterSensor, String> keyData =
                waterSensorSingleOutputStreamOperator.keyBy(value -> value.getId());

        //KeyBy窗口之后调用窗口
        SingleOutputStreamOperator<String> process =
                //TumblingEventTimeWindows 滚动事件时间窗口 一定是指定事件窗口 不然分配了水位线也没效果
                keyData.window(TumblingEventTimeWindows.of(Time.seconds(10)))
                        // 参数分别表示 输入类型 输出类型 key的类型 和 窗口类型
                        .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                            @Override
                            public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                                //通过上下文 拿到窗口的启停时间

                                final long start = context.window().getStart();
                                final long end = context.window().getEnd();
                                final String st = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss");
                                String en = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss");

                                //获取条数
                                long l = elements.spliterator().estimateSize();

                                out.collect("key=" + s + " 的窗口的时间[ " + st + "," + en + "] 包含 " + l + "  条数据: " + elements);
                            }
                        });


        process.print();

        env.execute();


    }
}
